package com.mercury.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mercury.user.entity.CenterUser;
import com.mercury.user.entity.VO.UserInfoVo;
import com.mercury.user.mapper.CenterUserMapper;
import com.mercury.user.service.CenterUserService;
import com.mercury.user.service.CenterUserTastedService;
import com.mercury.user.service.CenterUserWantService;
import com.mercury.util.DefaultValue;
import com.mercury.util.MD5;
import com.mercury.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import java.util.*;


/**
 *
 * @author 张伟娜，童雅婷
 * @since 2021-10-25
 */
@Service
public class CenterUserServiceImpl extends ServiceImpl<CenterUserMapper, CenterUser> implements CenterUserService {

    @Autowired
    private CenterUserWantService wantService;

    @Autowired
    private CenterUserTastedService tastedService;

    /**
     * @date 2021/10/28 8:36
     * @params centerUser
     * @return  CenterUser
     * @description 生成用户的uuid 并且将用户的密码加密
    */
    public CenterUser encryptionAndUUID(CenterUser centerUser) {
        String hex16 = MD5.encrypt(centerUser.getUserPassword());
        centerUser.setUserPassword(hex16);
        centerUser.setUserId(UUID.randomUUID().toString());
        return centerUser;
    }

    @Override
    public boolean addUser(CenterUser centerUser) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_tel", centerUser.getUserTel());
        List<CenterUser> centerUserList = (List<CenterUser>) this.listByMap(map);
        System.out.println(centerUserList.toString());
        if (centerUserList.size() != 0){
            return false;
        }
        centerUser = encryptionAndUUID(centerUser);
        centerUser.setIsDeleted(0);
        centerUser.setUserImg(DefaultValue.DEFAULT_HEAD_PORTRAIT);
        return this.save(centerUser);
    }

    @Override
    public UserInfoVo getUserInfo(CenterUser user) {
        QueryWrapper<CenterUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_tel",user.getUserTel());
        CenterUser centerUser = baseMapper.selectOne(wrapper);
        if(centerUser == null){
            return null;
        }
        String confirmPwd = MD5.encrypt(user.getUserPassword());
        if(confirmPwd.equals(centerUser.getUserPassword())){
            String userId = centerUser.getUserId();
            UserInfoVo userInfoVo = baseMapper.getUserInfo(userId);
            userInfoVo.setWantNum(wantService.getCount(userId));
            userInfoVo.setTastedNum(wantService.getCount(userId));
            return userInfoVo;
        }else{
            return null;
        }
    }

    @Override
    public UserInfoVo getInfo(String userId) {
        UserInfoVo userInfoVo = baseMapper.getUserInfo(userId);
        userInfoVo.setWantNum(wantService.getCount(userId));
        userInfoVo.setTastedNum(wantService.getCount(userId));
        return userInfoVo;
    }

    @Override
    public int setUserBeans(String userId) {
        CenterUser centerUser = baseMapper.selectById(userId);
        int beanNum = Integer.parseInt(Random.beanNums() + 1) % 5;
        centerUser.setCoffeeBean(centerUser.getCoffeeBean() + beanNum);
        return beanNum;
    }

}
